﻿using CodeTester;
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using Sunny.UI;
using MineCraft_Command_Editor.FileSystemIO;

namespace MineCraft_Command_Editor
{
    public partial class Main : UIForm
    {

        internal string localFilePath,ptr,pp;
        internal static List<string> file = new List<string>();
        internal static XmlNode oldChild;

        public Main()
        {
            InitializeComponent();
        }

        private void 新工程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Project.CreateProject cp = new Project.CreateProject();
            cp.Show();
        }

        public static void getFileName(string path)
        {
            DirectoryInfo root = new DirectoryInfo(path);
            foreach (FileInfo f in root.GetFiles())
            {
                //先分割以保证美观
                string path_name = f.Name.Split('.')[0];
                file.Add(path_name);
            }
        }

        private void 打开工程ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "选择项目工程文件";
            openFileDialog1.Filter = "MineCraft Function Project File(*.mfproj)|*.mfproj";
            openFileDialog1.FileName = "";
            DialogResult result = openFileDialog1.ShowDialog();
            if (result == DialogResult.OK)  //DialogResult为枚举类,OK为保存
            {
                //获得文件路径
                localFilePath = openFileDialog1.FileName.ToString();
                //取节点
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(localFilePath);
                //取路径
                oldChild = xmlDoc.SelectSingleNode("Project/Path");
                toolStripStatusLabel1.Text = $@"工程路径：{oldChild.InnerText}";
                Text = $"{oldChild.InnerText}";
                if (oldChild.InnerText.Length > 20)
                {
                    Text = $"...{oldChild.InnerText.Substring(20)}";
                }
                ptr = oldChild.InnerText + @"behavior_packs\functions";
                getFileName(oldChild.InnerText + @"behavior_packs\functions");
                foreach (var name in file)
                {
                    listBox1.Items.Add(name);
                }
            }
            //开始生成工程路径图
            //先获取工程名
            string[] gp = oldChild.InnerText.Split('\\');
            string pn = gp[gp.Length - 2];
            //项目主节点
            tree.Nodes.Add(pn);
            //工程文件
            tree.Nodes[0].Nodes.Add(pn + ".mfproj");
            //主文件夹
            tree.Nodes[0].Nodes.Add("behavior_packs");
            //向包下添加文件夹
            tree.Nodes[0].Nodes[1].Nodes.Add("entities");
            tree.Nodes[0].Nodes[1].Nodes.Add("functions");
            tree.Nodes[0].Nodes[1].Nodes.Add("manifest");
            //向函数添加函数
            //首先Get路径
            List<string> pf = GetFile.ListFiles(new DirectoryInfo(oldChild.InnerText + @"behavior_packs\functions\"));
            foreach (var item in pf)
            {
                string[] ptz = item.Split('\\');
                tree.Nodes[0].Nodes[1].Nodes[1].Nodes.Add($"{item.Split('\\')[ptz.Length - 1]}");
            }
            
        }

        private void Main_Load(object sender, EventArgs e)
        {
            openFileDialog1.AddExtension = true;
        }

  

        private void New_Func_Click(object sender, EventArgs e)
        {

            listBox1.Items.Clear();

            try
            {
                StreamWriter sw = new StreamWriter(ptr + $@"\{txt_FuncName.Text}.mcfunction");
                sw.Write(string.Empty);
                sw.Close();
                //重新加载

                file.Clear();
                getFileName(oldChild.InnerText + @"behavior_packs\functions");
                foreach (var name in file)
                {
                    listBox1.Items.Add(name);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show("未打开工程!",ex.Message,MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void tree_DoubleClick(object sender, EventArgs e)
        {
            MessageBox.Show($"您选中了{tree.SelectedNode}","选中",MessageBoxButtons.OK,MessageBoxIcon.Information);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void listBox1_DoubleClick(object sender, EventArgs e)
        {
            //打开文件
            try
            {
                int index = listBox1.SelectedIndex;
                string fileName = ptr + "\\" + listBox1.Items[index].ToString() + ".mcfunction";
                StreamReader sr = new StreamReader(fileName);
                string temp = sr.ReadToEnd();
                sr.Close();
                Form1 wcf = new Form1(temp,fileName);
                wcf.Show();
            }
            catch
            {
                MessageBox.Show("不是Minecraft Function文件!","打开失败",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
    }
}
